// 注意：这是一个vuex@3 的 module 对象，只能应用在Vue2项目中

import { getSpuImageList, getSpuSaleAttrList } from "@/api/product";

const state = {
  spuSaleAttrListItem: {
    spuName: "",
    description: "",
    tmId: undefined,
    baseSaleAttrId: undefined,
    spuImageList: [],
    spuSaleAttrList: [],
  },
};
const mutations = {
  UPDATE_SPUSALEATTRLISTITEM(state, spuSaleAttrListItem) {
    state.spuSaleAttrListItem = spuSaleAttrListItem;
  },
  RESET_SPUSALEATTRLISTITEM(state) {
    state.spuSaleAttrListItem = {
      spuName: "",
      description: "",
      tmId: undefined,
      baseSaleAttrId: undefined,
      spuImageList: [],
      spuSaleAttrList: [],
    };
  },
  UPDATE_SPUIMAGELIST(state, spuImageList) {
    state.spuSaleAttrListItem.spuImageList = spuImageList;
  },
  UPDATE_SPUSALEATTRLIST(state, spuSaleAttrList) {
    state.spuSaleAttrListItem.spuSaleAttrList = spuSaleAttrList;
  },
};

const actions = {
  async getSpuImageList({ commit }, spuId) {
    const res = await getSpuImageList(spuId);
    const newSpuImageList = res.data.map((item) => ({
      name: item.imgName,
      response: { data: item.imgUrl },
      url: item.imgUrl,
      // status: "success",
    }));
    commit("UPDATE_SPUIMAGELIST", newSpuImageList);
  },
  async getSpuSaleAttrList({ commit }, spuId) {
    const res = await getSpuSaleAttrList(spuId);
    const newSpuSaleAttrList = res.data.map(item=>({
      ...item,
      isShow:false
    }))
    commit("UPDATE_SPUSALEATTRLIST", newSpuSaleAttrList);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
